﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using System.Text;

public partial class Task_EditTaskDlg : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.ParseIntQueryString(Request, "id", true, ref taskId))
        {
            lblMsg.Text = "参数不正确";
            return;
        }

        if (!IsPostBack)
        {
            DoDataBind();
        }
    }

    int taskId;
    int EntityVersion
    {
        get { return ViewState["EntityVersion"] == null ? -1 : (int)ViewState["EntityVersion"]; }
        set { ViewState["EntityVersion"] = value; }
    }

    private void DoDataBind()
    {
        ITaskManager taskMgr = ManagerFactory.CreateTaskManager();
        TaskInfo task = taskMgr.GetTask(CallContext, taskId);
        if (task != null)
        {
            ucTask.EditTask(task);
            // TODO: add version here
        }
        else
        {
            lblMsg.Text = "任务不存在，可能已经被删除";
        }
    }

    protected void btOK_Click(object sender, EventArgs e)
    {
        ITaskManager taskMgr = ManagerFactory.CreateTaskManager();
        TaskInfo task = taskMgr.GetTask(CallContext, taskId);
        if (task != null)
        {
            ucTask.GetTask(task);
            task.AID = taskId;
            // TODO: enable user id here
            //task.ModifyUserID = CallContext.UserID;
            task.ModifyDateTime = DateTime.Now;
            if (taskMgr.UpdateTask(CallContext, task))
            {
                lblMsg.Text = "保存成功, 3秒钟将关闭对话框。";
                GenerateCloseDialogScript();
            }
            else
            {
                lblMsg.Text = "保存失败";
            }
        }
        else
        {
            lblMsg.Text = "任务不存在，可能已经被删除";
        }
    }

    void GenerateCloseDialogScript()
    {
        StringBuilder sbscript = new StringBuilder();
        sbscript.Append("setTimeout(function(){window.parent.document.onDialogCallBack(1, 'succeed',");
        sbscript.Append("null);}, 3000);");

        btOK.Enabled = false;
        ClientScript.RegisterStartupScript(GetType(),
            "BCF145CA-3290-43A0-9543-D4EB12506FB9",
            sbscript.ToString()
            , true);
    }
}
